/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.dataaccess.hibernate;

import cms.dataaccess.UserDAO;
import cms.exceptions.HibernateAccessLayerException;
import dto.User;
import java.util.List;

/**
 *
 * @author eichawsan
 */
public class UserDAOImpl extends AbstractDAO implements UserDAO{

    public UserDAOImpl(){                                                                                   
        super();
    }
    @Override
      public User findUser(String UserName) throws HibernateAccessLayerException {
      try {
            // Logger.get// Logger(getClass().getName()).log(Level.INFO, "Finding User" + UserName);
            User p = (User) super.find(User.class, UserName);
            return p;
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
        
    }
    
    @Override
    public List<User> findAllUser() throws HibernateAccessLayerException {
        try {
            List result =  super.findAll(User.class);
            return (result);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
        
    }
    
    @Override
    public void insertUser(User usr) throws HibernateAccessLayerException {
        try {
            // Logger.get// Logger(getClass().getName()).log(Level.INFO, "Inserting User" + usr.getUserName());
            super.saveOrUpdate(usr);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }
    
    @Override
    public void updateUser(User usr) throws HibernateAccessLayerException {
         try {
            // Logger.get// Logger(getClass().getName()).log(Level.INFO, "Updating User" + usr.getUserName());
            super.saveOrUpdate(usr);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
        
    }
    
    @Override
    public void deleteUser(User usr) throws HibernateAccessLayerException {
        try {
            // Logger.get// Logger(getClass().getName()).log(Level.INFO, "Deleting User" + usr.getUserName());
            super.delete(usr);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public List<User> findByCustomizeQuery(String queryString) throws HibernateAccessLayerException {
          try {  
           return (List<User>)this.findByCustomizeQuery(User.class,queryString);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    
    }
    @Override
    public boolean Login(String username, String password, String role) {
          List ls = this.findByCustomizeQuery(User.class, "userName='"+username+"' and password=MD5('"+password+"')");
          if (ls != null && ls.size() == 1)
          {
              return true; 
          }
          return false;
    }
    
}
